<template>
  <div class="app-container">
    <el-input class="marginRB10" v-model="listQuery.lisence_id" placeholder="请输入资质编号" style="width:200px;"
              clearable></el-input>
    <el-input class="marginRB10" v-model="listQuery.user_code" placeholder="请输入用户编号" style="width:200px;"
              clearable></el-input>
    <el-select filterable v-model="listQuery.status" class="filter-item marginRB10" placeholder='请选择'
               style="width: 200px;">
      <el-option v-for="item in flagList1" :key="item.key" :label="item.lab" :value="item.key"></el-option>
    </el-select>
    <el-button type="primary" @click="searchList" icon="el-icon-search"   class="marginRB10">搜索</el-button>

    <el-table ref="dataList" :data="menuList" style="margin-bottom: 20px;" row-key="id" border highlight-current-row>
      <el-table-column prop="id" label="id"></el-table-column>
      <el-table-column prop="license_name" label="资质名称"></el-table-column>
      <el-table-column prop="avatar" label="资质头像">
        <template slot-scope="scope">
          <el-popover placement="right" max-width="220" trigger="hover">
            <img :src="scope.row.avatar" alt="" style="max-width: 200px;max-height: 200px">
            <img slot="reference" :src="scope.row.avatar" alt="" style="width: 50px;">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" min-width="100">
        <template slot-scope="scope">
          {{ scope.row.status === 1 ? '待审核' : '' }}
          {{ scope.row.status === 2 ? '审核失败' : '' }}
          {{ scope.row.status === 3 ? '审核成功' : '' }}
        </template>
      </el-table-column>
      <el-table-column prop="lisence_pic" label="资质图片">
        <template slot-scope="scope">
          <el-popover placement="right" max-width="220" trigger="hover">
            <img :src="scope.row.lisence_pic" alt="" style="max-width: 200px;max-height: 200px">
            <img slot="reference" :src="scope.row.lisence_pic" alt="" style="width: 50px;">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="voice" label="声音地址">
        <template slot-scope="scope">
          <svg-icon icon-class="stop" @click="videoPause()" v-if="isAmr && isAmrId===scope.row.id"
                    style="font-size: 30px;"/>
          <svg-icon icon-class="play" @click="videoPlay(scope.row.voice, scope.row.id)" v-else
                    style="font-size: 30px;"/>
        </template>
      </el-table-column>
      <el-table-column prop="memo" label="接单说明"></el-table-column>
      <el-table-column prop="admin_id" label="管理员编号"></el-table-column>
      <el-table-column prop="verify_memo" label="审核意见"></el-table-column>
      <el-table-column prop="add_time" label="提交时间"></el-table-column>
      <el-table-column prop="update_time" label="审核时间"></el-table-column>
      <el-table-column prop="user_code" label="用户编号"></el-table-column>
      <el-table-column prop="duration" label="录音时长"></el-table-column>
      <el-table-column prop="nickname" label="用户昵称"></el-table-column>

      <el-table-column min-width="180" label="操作">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status === 1" type="primary" size="mini" @click="handleUpdate(scope.row)" plain>
            通过
          </el-button>
          <el-button v-if="scope.row.status === 1 " type="warning" size="mini" @click="handleUpdate2(scope.row)" plain>
            编辑
          </el-button>
          <!--          <cus-del-btn @ok="handleDelete(scope.row)"></cus-del-btn>-->
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <cus-pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.perPage"
                    @pagination="getList"/>

    <el-dialog v-dialogDrag title="日志" :visible.sync="dialogFormVisible" @close="handleDialogClose">
      <el-table ref="dataList" :data="form" row-key="id" border highlight-current-row>
        <el-table-column prop="order_id" label="订单id"></el-table-column>
        <el-table-column prop="add_time" label="添加时间"></el-table-column>
        <el-table-column prop="is_auto" label="操作类别">
          <template slot-scope="scope">
            {{ scope.row.is_auto === 1 ? '人工' : '' }}
            {{ scope.row.is_auto === 2 ? '机器' : '' }}
          </template>
        </el-table-column>
        <el-table-column prop="type" label="订单状态" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.type === 1 ? '待接单' : '' }}
            {{ scope.row.type === 2 ? '已取消' : '' }}
          </template>
        </el-table-column>
        <el-table-column prop="memo" label="操作详细备注"></el-table-column>
        <el-table-column prop="admin_id" label="管理员id"></el-table-column>
        <el-table-column prop="admin_name" label="管理员名称"></el-table-column>

      </el-table>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="dialogFormVisible = false">
          取 消
        </el-button>
      </div>
    </el-dialog>

    <el-dialog v-dialogDrag title="编辑不显示资质审核" :visible.sync="dialogFormVisible2">
      <el-form ref="dataForm" :rules="rules2" :model="form2" label-position="right" label-width="120px"
               style="width: 400px; margin: 0 auto;">
        <el-form-item label="标签ID" prop="tag_ids">
          <el-input v-model="form2.tag_ids" placeholder="请输入标签ID"/>
        </el-form-item>
        <el-form-item label="标签镜像" prop="tag_image">
          <el-input v-model="form2.tag_image" placeholder="请输入标签镜像"/>
        </el-form-item>
        <el-form-item label="必须的tag_id" prop="unique_tag">
          <el-input v-model="form2.unique_tag" placeholder="请输入tag_id"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="dialogFormVisible2 = false">
          取 消
        </el-button>
        <el-button type="primary" @click="submitForm2()">
          确定
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  deleteData,
  listData,
  logListData,
  refundData,
  lisenceApplyData,
  lisenceApply
} from '@/api/qualifications/qualificationsMachine'

let BenzAMRRecorder = require('benz-amr-recorder');
export default {
  data() {
    return {
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1,
        perPage: 10,
        lisence_id: undefined,
        user_code: undefined,
        status: undefined,
      },
      menuList: [],
      dialogFormVisible: false,
      dialogFormVisible2: false,
      dialogStatus: '',
      titleMap: {
        update: '编辑',
        create: '增加'
      },
      flagList1: [{key: undefined, lab: '全部状态'}, {key: '1', lab: '待审核'}, {key: '2', lab: '审核失败'}, {
        key: '3',
        lab: '审核成功'
      },],
      form: {
        id: undefined,
        name: undefined,
      },
      form2: {
        id: undefined,
        tag_ids: undefined,
        tag_image: undefined,
        unique_tag: undefined,
      },
      amr: undefined,
      isAmr: false,
      isAmrId: 0,
      rules: {
        name: [{required: true, message: '请输入名字', trigger: 'blur'}],
      },
      rules2: {
        tag_ids: [{required: true, message: '请输入名字', trigger: 'blur'}],
        tag_image: [{required: true, message: '请输入名字', trigger: 'blur'}],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 点击查询
    searchSubmit() {
      this.listQuery.page = 1;
      this.getList()
    },
    searchList() {

    },
    getList() {
      this.listLoading = true
      for (let key in this.listQuery) {
        this.listQuery[key] = this.listQuery[key] === "" ? undefined : this.listQuery[key]
      }
      listData(this.listQuery).then(response => {
        this.menuList = response.data.list
        this.total = response.data.meta.total;
        this.listLoading = false
      })
    },
    handleCreate(row) {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleUpdate(row) {
      let data = {id: row.id}
      logListData(data).then(response => {
        // this.form = response.data
        // this.dialogFormVisible = true
        if (response.code === 200) {
          this.getList()
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          });
        }
      })
    },
    handleUpdate2(row) {
      this.dialogStatus = 'update'
      this.dialogFormVisible2 = true
      let data = {id: row.id}
      lisenceApplyData(data).then(response => {
        if (response.code === 200) {
          this.form2.id = response.data.id
          this.form2.tag_ids = response.data.tag_ids
          this.form2.tag_image = response.data.tag_image
          this.form2.unique_tag = response.data.unique_tag
        }
      })
    },
    handleDelete(row) {
      let data = {ids: row.id}
      deleteData(data).then(response => {
        if (response.code === 200) {
          this.getList()
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          });
        }
      })
    },
    submitForm() {
      this.$refs['dataForm'].validate(valid => {
        if (valid && this.dialogStatus === 'create') {
          let formData = new FormData();
          for (let item in this.form) {
            this.form[item] === undefined ? '' : formData.append(item, this.form[item]);
          }
          saveData(formData).then(response => {
            if (response.code === 200) {
              this.getList()
              this.$notify({
                type: 'success',
                message: '操作成功',
                position: 'bottom-right',
                showClose: false,
                duration: 3000
              });
              this.dialogFormVisible = false
            } else {
              this.$message({
                message: response.message,
                type: 'error'
              });
            }
          })
        } else if (valid && this.dialogStatus === 'update') {
          let formData = new FormData();
          for (let item in this.form) {
            this.form[item] === undefined ? '' : formData.append(item, this.form[item]);
          }
          updateData(formData).then(response => {
            if (response.code === 200) {
              this.getList()
              this.submitOk(response.message)
              this.dialogFormVisible = false
            } else {
              this.$message({
                message: response.message,
                type: 'error'
              });
            }
          })
        }
      })
    },
    submitForm2() {
      let formData = new FormData();
      for (let item in this.form2) {
        this.form2[item] === undefined ? '' : formData.append(item, this.form2[item]);
      }
      lisenceApply(formData).then(response => {
        this.getList()
        this.dialogFormVisible2 = false
      })
    },
    resetForm() {
      this.form = {
        id: undefined,
        name: undefined,
      }
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose() {
      if (this.$refs['dataForm']) {
        this.$refs['dataForm'].clearValidate() // 清除整个表单的校验
      }
    },
    videoPause() {
      this.isAmr = false
      this.amr.stop()
    },
    videoPlay(url, id) {
      this.isAmr = true
      this.isAmrId = id
      let that = this
      if (this.amr !== undefined) {
        this.amr.stop()
      }
      this.amr = new BenzAMRRecorder()
      this.amr.initWithUrl(url).then(function () {
        that.amr.onAutoEnded(function () {
          that.isAmr = false
        });
        that.amr.play()
      })
    },
  }
}
</script>
